﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Public
{
    public partial class mfForumInfo : msBsc.Cls.mbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 得到论坛
        /// </summary>
        private void getForumInfo()
        {
            string forumId = this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(forumId))
            {
                this.ExecJScript(@"window.location.href = ""/ms_ForumList.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts = new AppLogic.DataAlc.CBizSourcePosts())
            {
                DataSet ds = sourcePosts.GetPostsForView(forumId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"setForumInfo(" + retJson + ")");
            }
        }

        /// <summary>
        /// 得到论坛回复
        /// </summary>
        private void getReplyList()
        {
            string forumId = this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(forumId))
            {
                this.ExecJScript(@"window.location.href = ""/ms_ForumList.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts = new AppLogic.DataAlc.CBizSourcePosts())
            {
                string pIndex = this.GetParamHtValue("pIndex");
                string pRows = this.GetParamHtValue("pRows");
                int pageIndex = 1;
                int pageRows = 11;
                int.TryParse(pIndex, out pageIndex);
                int.TryParse(pRows, out pageRows);
                DataSet ds = sourcePosts.GetRevocerForPageview(pageRows, pageIndex, forumId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"$('#titleReply').hide();$('#nextPage').hide();");
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"setReplyList(" + retJson + ",'" + forumId + "');setPage(" + Convert.ToInt32(ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString()) + "," + pIndex + ");");
            }
        }

        /// <summary>
        /// 回复论坛
        /// </summary>
        private void setReply()
        {
            string replyInfo = this.GetParamHtValue("replyInfo");
            string replyId = this.GetParamHtValue("replyId");
            string djackId = msBsc.SvRef.UserInfo.Operator;
            string forumId = this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(djackId))
            {
                this.ExecJScript(@"window.location.href = ""/mp_UserLogin.htm?ReturnUrl=""+window.location.pathname+window.location.search");
                return;
            }
            if (string.IsNullOrEmpty(replyInfo))
            {
                this.Alert("回复内容不能为空");
                return;
            }
            if (string.IsNullOrEmpty(replyId))
            {
                this.Alert("回复贴Id不能为空");
                return;
            }
            if (string.IsNullOrEmpty(djackId))
            {
                this.ExecJScript(@"window.location.href = ""/mp_UserLogin.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizRecoverPosts recoverPosts = new AppLogic.DataAlc.CBizRecoverPosts())
            {
                string[] ret = recoverPosts.AddRecoverPosts(replyInfo, djackId, replyId, forumId);
                if (ret[0] == "OK")
                {
                    this.ExecJScript(@"window.location.href = window.location.href;");
                }
                else
                {
                    this.Alert(ret[1]);
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getForumInfo();
                base.DoAction();
                //getReplyList();
            }
            else if (this.strAction == "reply")
            {
                setReply();
            }
            else if (this.strAction == "getReply")
            {
                getReplyList();
            }
            else if (this.strAction == "showReply")
            {
                string djackId = msBsc.SvRef.UserInfo.Operator;
                string postsId = this.GetParamHtValue("postsId");
                if (string.IsNullOrEmpty(djackId))
                {
                    this.ExecJScript(@"window.location.href = ""/mp_UserLogin.htm?ReturnUrl=""+window.location.pathname+window.location.search;");
                }
                else
                {
                    this.ExecJScript(@"$(""#replyInfo .btn_replydiv2"").hide();
                $("".child_reply[data-value='"+postsId+@"']"").parent(""div"").next(""div"").slideToggle(""normal"");");
                }
            }
            else if (this.strAction == "showComment")
            {
                string djackId = msBsc.SvRef.UserInfo.Operator;
                string postsId = this.GetParamHtValue("postsId");
                if (string.IsNullOrEmpty(djackId))
                {
                    this.ExecJScript(@"window.location.href = ""/mp_UserLogin.htm?ReturnUrl=""+window.location.pathname+window.location.search;");
                }
                else
                {
                    this.ExecJScript(@"$(""#comment"").focus();");
                }
            }
        }
    }
}